//
//  BrandDetailHeaderCell.swift
//  ZhiTing
//
//  Created by iMac on 2021/2/26.
//

import UIKit

class PluginCell: UITableViewCell, ReusableView {
    var installPluginCallback: (() -> ())?
    var updatePluginCallback: (() -> ())?
    var deletePluginCallback: (() -> ())?

    var plugin: Plugin? {
        didSet {
            guard let plugin = plugin else { return }
            nameLabel.text = plugin.name
            versionLabel.text = "版本号：".localizedString + plugin.version
            descriptionLabel.text = plugin.info
            updateTimeLabel.text = "版本更新日期：".localizedString + String.timeStampToString(timeStamp: plugin.update_at)
            
            let lines: CGFloat = plugin.info.height(thatFitsWidth: Screen.screenWidth - 150, font: .font(size: 12, type: .regular)) /  UIFont.font(size: 12, type: .regular).lineHeight

            if descriptionLabel.numberOfLines == 3 && lines > 3 {
                dotLabel.isHidden = false
            } else {
                dotLabel.isHidden = true
            }
            
            
            if plugin.is_added {
                if plugin.is_newest {
                    status = .added
                } else {
                    status = .needUpdate
                }
            } else {
                status = .normal
            }
            
            if plugin.is_updating {
                status = .updating
            }
        }
    }

    enum Status {
        case added
        case normal
        case needUpdate
        case updating
    }
    
    var status: Status = .normal {
        didSet {
            updateStatus(by: status)
        }
    }

    lazy var nameLabel = Label().then {
        $0.font = .font(size: 14, type: .bold)
        $0.textColor = .custom(.black_3f4663)
        $0.text = "Unknown Plugin"
        $0.numberOfLines = 0
    }
    
    lazy var versionLabel = Label().then {
        $0.font = .font(size: 12, type: .bold)
        $0.textColor = .custom(.gray_94a5be)
        $0.text = "Version: v1.0.0"
    }
    
    lazy var updateTimeLabel = Label().then {
        $0.font = .font(size: 12, type: .bold)
        $0.textColor = .custom(.gray_94a5be)
        $0.text = "2020-01-01"
    }
    
    lazy var descriptionLabel = Label().then {
        $0.font = .font(size: 12, type: .regular)
        $0.textColor = .custom(.black_3f4663)
        $0.numberOfLines = 3
        $0.text = "Plugin Description."
    }
    
    lazy var dotLabel = Label().then {
        $0.font = .font(size: 12, type: .regular)
        $0.textColor = .custom(.black_3f4663)
        $0.numberOfLines = 1
        $0.text = "..."
    }
    
    lazy var installButton = Button().then {
        $0.setTitle("添加".localizedString, for: .normal)
        switch getCurrentLanguage() {
        case .chinese:
            $0.titleLabel?.font = .font(size: 14, type: .regular)
        case .english:
            $0.titleLabel?.font = .font(size: 10, type: .regular)
        }
        $0.setTitleColor(.custom(.blue_2da3f6), for: .normal)
        $0.clickCallBack = { [weak self] _ in
            self?.installPluginCallback?()
        }
    }
    
    lazy var updateButton = Button().then {
        $0.setTitle("更新".localizedString, for: .normal)
        switch getCurrentLanguage() {
        case .chinese:
            $0.titleLabel?.font = .font(size: 14, type: .regular)
        case .english:
            $0.titleLabel?.font = .font(size: 10, type: .regular)
        }
        $0.setTitleColor(.custom(.blue_2da3f6), for: .normal)
        $0.clickCallBack = { [weak self] _ in
            self?.updatePluginCallback?()
        }
    }
    
    lazy var deleteButton = Button().then {
        $0.setTitle("删除".localizedString, for: .normal)
        switch getCurrentLanguage() {
        case .chinese:
            $0.titleLabel?.font = .font(size: 14, type: .regular)
        case .english:
            $0.titleLabel?.font = .font(size: 10, type: .regular)
        }
        $0.setTitleColor(.custom(.blue_2da3f6), for: .normal)
        $0.clickCallBack = { [weak self] _ in
            self?.deletePluginCallback?()
        }
    }
    
    lazy var progressView = CircularProgress(frame: CGRect(x: 0, y: 0, width: 30, height: 30)).then { $0.isHidden = true }

    lazy var line = UIView().then {
        $0.backgroundColor = .custom(.gray_eeeeee)
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
        setConstrains()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupViews() {
        contentView.backgroundColor = .custom(.white_ffffff)
        contentView.addSubview(nameLabel)
        contentView.addSubview(versionLabel)
        contentView.addSubview(updateTimeLabel)
        contentView.addSubview(descriptionLabel)
        contentView.addSubview(dotLabel)
        contentView.addSubview(installButton)
        contentView.addSubview(updateButton)
        contentView.addSubview(deleteButton)
        contentView.addSubview(progressView)
        contentView.addSubview(line)
    }
    
    func setConstrains() {
        nameLabel.snp.makeConstraints {
            $0.top.equalToSuperview().offset(15.5)
            $0.left.equalToSuperview().offset(20.5)
            $0.right.equalToSuperview().offset(-100)
        }
        
        versionLabel.snp.makeConstraints {
            $0.left.equalTo(nameLabel.snp.left)
            $0.top.equalTo(nameLabel.snp.bottom)
        }
        
        updateTimeLabel.snp.makeConstraints {
            $0.top.equalTo(versionLabel)
            $0.left.equalTo(versionLabel.snp.right).offset(ZTScaleValue(45))
        }
        
        descriptionLabel.snp.makeConstraints {
            $0.top.equalTo(versionLabel.snp.bottom).offset(6)
            $0.left.equalTo(nameLabel.snp.left)
            $0.right.equalToSuperview().offset(-24.5)
        }
        
        dotLabel.snp.makeConstraints {
            $0.top.equalTo(descriptionLabel.snp.bottom).offset(-5)
            $0.left.equalTo(nameLabel.snp.left)
        }
        
        line.snp.makeConstraints {
            $0.height.equalTo(0.5)
            $0.top.equalTo(descriptionLabel.snp.bottom).offset(15.5)
            $0.left.equalToSuperview().offset(20.5)
            $0.right.equalToSuperview().offset(-20.5)
            $0.bottom.equalToSuperview()
        }
    }

}

extension PluginCell {
    private func updateStatus(by status: Status) {
        installButton.isHidden = true
        updateButton.isHidden = true
        deleteButton.isHidden = true
        progressView.isHidden = true
        installButton.snp.removeConstraints()
        updateButton.snp.removeConstraints()
        deleteButton.snp.removeConstraints()
        progressView.snp.removeConstraints()
        progressView.stopRotating()
        
        switch status {
        case .normal:
            installButton.isHidden = false
            installButton.snp.makeConstraints {
                $0.top.equalToSuperview().offset(16.5)
                $0.right.equalToSuperview().offset(-21)
                $0.height.equalTo(15)
                $0.width.equalTo(30)
            }
        case .added:
            deleteButton.isHidden = false
            deleteButton.snp.makeConstraints {
                $0.top.equalToSuperview().offset(16.5)
                $0.right.equalToSuperview().offset(-21)
                $0.height.equalTo(15)
                $0.width.equalTo(30)
            }
        case .needUpdate:
            deleteButton.isHidden = false
            updateButton.isHidden = false
            updateButton.snp.makeConstraints {
                $0.top.equalToSuperview().offset(16.5)
                $0.right.equalToSuperview().offset(-21)
                $0.height.equalTo(15)
                $0.width.equalTo(35)
            }
            
            deleteButton.snp.makeConstraints {
                $0.top.equalToSuperview().offset(16.5)
                $0.right.equalTo(updateButton.snp.left).offset(-5)
                $0.height.equalTo(15)
                $0.width.equalTo(35)
            }
        case .updating:
            progressView.isHidden = false
            progressView.startRotating()
            
            progressView.snp.makeConstraints {
                $0.width.height.equalTo(20)
                $0.top.equalToSuperview().offset(16.5)
                $0.right.equalToSuperview().offset(-21)
            }
            
        }

    }

}
